package com.eqdd.yiqidian.ui.mine.pizhun.Fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.yiqidian.CoolRefreshRecyclerViewFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ListResultBean;
import com.eqdd.yiqidian.bean.rebuild.TravelListResultBean;
import com.eqdd.yiqidian.bean.rebuild.TravelListResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.ui.mine.pizhun.OverTimenfoActivity;
import com.eqdd.yiqidian.ui.mine.pizhun.TravelInfoActivity;
import com.eqdd.yiqidian.utils.DateUtil;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import org.reactivestreams.Publisher;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * Created by lvzhihao on 17-7-19.
 */

public class TravelCheckFragment extends BaseFragment {
    CoolRefreshRecyclerViewFragmentCustom dataBinding;
    private SlimAdapterEx<TravelListResultBean> slimAdapterEx;
    private int status;
    private ModelRx2DataSource<TravelListResultBean> dataSource;
    private MVCCoolHelper<List<TravelListResultBean>> mvcHelper;
    private int pageNum = 0;
    private int who;

    @Override
    protected void setView() {
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_12, new SlimInjector<TravelListResultBean>() {
                @Override
                public void onInject(TravelListResultBean data, IViewInjector injector) {
                    injector.text(R.id.tv_upper_left_content, "申请时间")
                            .text(R.id.tv_upper_right_content, data.getCreateTime())
                            .text(R.id.tv_down_left_content, "")
                            .text(R.id.tv_down_right_content, data.getTravelAddress());
                }
            })
                    .attachTo(dataBinding.recyclerView);
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        Bundle bundle = new Bundle();
                        bundle.putString("travelId", slimAdapterEx.getData().get(position).getId() + "");
                        bundle.putInt(Config.STATUS, status);
                        bundle.putInt(Config.WHO, who);

                        ((BaseActivity) getActivity()).startActivityForResult(TravelInfoActivity.class, bundle, position);
                    });
            dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
                @Override
                public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                    return OkGo.<HttpResult<ListResultBean<TravelListResultBean>>>post(HttpConfig.BASE_URL + (who == 2 ? HttpConfig.TRAVEL_HR_LIST : HttpConfig.TRAVEL_CHECK_LIST))
                            .params("page", pageNum)
                            .params("companyId", GreenDaoUtil.getUser().getCompanyId())
                            .params("userGuid", GreenDaoUtil.getUser().getGuid())
                            .params("type", status)
                            .converter(new JsonConverter<HttpResult<ListResultBean<TravelListResultBean>>>() {
                                @Override
                                public void test() {
                                    super.test();
                                }
                            }).adapt(new FlowableBody<HttpResult<ListResultBean<TravelListResultBean>>>())
                            .flatMap(new Function<HttpResult<ListResultBean<TravelListResultBean>>, Publisher<List<TravelListResultBean>>>() {
                                @Override
                                public Publisher<List<TravelListResultBean>> apply(@NonNull HttpResult<ListResultBean<TravelListResultBean>> noticeWraperResultBeanHttpResult) throws Exception {
                                    pageNum = page == 1 ? 0 : noticeWraperResultBeanHttpResult.getItems().getPage();
                                    if (noticeWraperResultBeanHttpResult.getStatus() == 200) {
                                        return Flowable.just(noticeWraperResultBeanHttpResult.getItems().getList() == null ? new ArrayList<TravelListResultBean>() : noticeWraperResultBeanHttpResult.getItems().getList());
                                    } else {
                                        ToastUtil.showShort(noticeWraperResultBeanHttpResult.getMsg());
                                        getActivity().finish();
                                        return Flowable.just(new ArrayList<>());
                                    }
                                }
                            });

                }
            });
            slimAdapterEx.updateData(new ArrayList<>());
            mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
            mvcHelper.setDataSource(dataSource);
            mvcHelper.setAdapter(slimAdapterEx);
            mvcHelper.refresh();
        }
    }

    @Override
    protected void initData() {

        status = getArguments().getInt(Config.STATUS);
        who = getArguments().getInt(Config.WHO);
    }


    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_cool_recycler_view, null, false);

    }

    @Override
    public void onClick(View v) {

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            slimAdapterEx.getData().remove(requestCode);
            slimAdapterEx.notifyItemRemoved(requestCode);
        }
    }
}
